package com.tma.internship.activities;

import java.util.List;

import com.tma.internship.ClassesInfo.BookmarkAdapter;
import com.tma.internship.ClassesInfo.CommentInfo;
import com.tma.internship.ClassesInfo.ControlBookMark;

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class BookmarkActivity extends Activity {

	
	private ListView list;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_bookmark);
		List<CommentInfo> listCmtInfos = (List<CommentInfo>) getIntent().getSerializableExtra("list");
		bookName = getIntent().getExtras().getString("book");
		if (listCmtInfos == null) return;
		BookmarkAdapter bAdapter = new BookmarkAdapter(this, R.layout.bookmark_item, listCmtInfos);
		list = (ListView) findViewById(R.id.list_bookmark);
		list.setAdapter(bAdapter);
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapter, View v, int position,
					long id) {
				// TODO Auto-generated method stub
				CommentInfo cmtInfo = (CommentInfo) adapter.getItemAtPosition(position);
				new ControlBookmarkDialog(BookmarkActivity.this, cmtInfo, (BookmarkAdapter) adapter.getAdapter()).show();
			}
		});
	}
	private String bookName;

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.bookmark_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()){
		case R.id.back:
			finish();
			return true;
		case R.id.delete_all:
			ControlBookMark ctr = new ControlBookMark(this);
			if (bookName != null) ctr.deleteCmtOnBook(bookName);
			else ctr.deleteAll();
			list.setAdapter(null);
			return true;
		default:return super.onMenuItemSelected(featureId, item);
		}
	}
	
}
